<template>
    <div class="app-container">
        <uc-table url="/admin/task/getTaskPage" name="任务管理" ref="taskTable" pageable table-border :search-form="searchForm">
            <template slot="searchForm">
                <el-form size="mini" :inline="true" :model="searchForm" class="demo-form-inline filter-container">
                    <!--搜索输入框-->
                    <el-form-item>
                        <el-input type="text" v-model="searchForm.name" placeholder="请输入任务名称" clearable @clear="clearInput()"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-select v-model="searchForm.enterpriseId" placeholder="请选择主体" style="width: 100%" clearable  @clear="clearInput1()">
                            <el-option v-for="(item,index) in enterpriseDominantList" :key="index"  :value="item.ucId" :label="item.name"></el-option>
                        </el-select>
                    </el-form-item>
                    <!--搜索按钮--新增按钮-->
                    <el-form-item>
                        <el-button type="primary" icon="el-icon-search" class="filter-item" @click="handleSearch()">查询</el-button>
                        <el-button type="success" icon="el-icon-plus" class="filter-item" @click="handleAdd()">新 增</el-button>
                    </el-form-item>
                </el-form>
            </template>
            <template slot="columns">
                <el-table-column prop="enterpriseName" label="所属主体" align="left" :show-overflow-tooltip="true"></el-table-column>
                <el-table-column prop="disPlayName" label="任务名称" align="left" :show-overflow-tooltip="true"></el-table-column>
                <el-table-column prop="content" label="任务内容" align="left" :show-overflow-tooltip="true"></el-table-column>
                <el-table-column prop="type" label="任务类型" align="left" :show-overflow-tooltip="true">
                    <template slot-scope="scope">
                        {{scope.row.type | taskType}}
                    </template>
                </el-table-column>
                <el-table-column label="任务状态" prop="status">
                    <template slot-scope="scope">
                        <el-tooltip class="item" effect="dark" content="点击切换任务状态" placement="top">
                            <el-tag class="pointer" @click="handleChangStatus(scope.row)" :type="scope.row.status?'success':'danger'">{{scope.row.status?"开启":"关闭"}}</el-tag>
                        </el-tooltip>
                    </template>
                </el-table-column>
                <el-table-column prop="address" label="操作" width="220" align="left" class-name="small-padding fixed-width">
                    <template slot-scope="scope">
                        <!--编辑按钮-->
                        <el-button size="mini" icon="el-icon-edit" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
                        <el-button size="mini" icon="el-icon-info" type="success" @click="handleInfo(scope.row)">详情</el-button>
                        <el-button size="mini" type="danger" icon="el-icon-delete" :loading="deleteLoading" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </template>
        </uc-table>
    </div>
</template>

<script>
import UcTable from "@/components/UcTable/index.vue";
import {deleteInTask, updateInTask} from "@/api/taskinfo";
import {getInEnterpriseDominantList} from "@/api/enterpriseDominant";
import { mapGetters } from "vuex";


export default {
    name: "taskList",
    components: {UcTable},
    data(){
        return{
            searchForm:{
                deleted:false,
            },
            deleteLoading:false,
            enterpriseDominantList:[]
        }
    },
    created() {
        this.getInEnterpriseDominantList()
    },
    computed:{
        ...mapGetters(["refresh"]),
    },
    methods:{
        handleSearch(){
            this.$refs.taskTable.getTableData(1)
        },
        handleAdd(){
            this.$router.push("/taskManage/task/edit")
        },
        handleEdit(val){
            this.$router.push("/taskManage/taskEdit/"+val.ucId)
        },
        handleInfo(val){
            this.$router.push("/taskManage/taskInfo/"+val.ucId)
        },
        clearInput(){
            //清除输入框
            this.searchForm.name = null
            this.handleSearch()
        },
        clearInput1(){
            //清除企业下拉框
            this.searchForm.enterpriseId = null
            this.handleSearch()
        },
        handleDelete(val){
            this.$confirm('您确定要执行删除'+val.disPlayName+'这个任务吗？', '提醒信息', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.deleteLoading = true
                deleteInTask(val).then(res=>{
                    this.deleteLoading = false
                    this.$notify.success({
                        title: '成功',
                        message: "删除任务成功"
                    })
                    this.handleSearch()
                }).catch(res=>{
                    this.deleteLoading = false
                    this.$notify.error({
                        title: '失败',
                        message: res.msg
                    })
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        getInEnterpriseDominantList(){
            getInEnterpriseDominantList({deleted:false}).then(res=>{
                this.enterpriseDominantList = res.data
            }).catch(error=>{

            })
        },
        //变更任务状态
        handleChangStatus(val){
            this.$confirm('您确定要执行变更' + val.name + '这个任务状态吗？', '提醒信息', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let obj = Object.assign({},val)
                obj.status = !obj.status
                updateInTask(obj).then(res => {
                    this.$notify.success({
                        title: '成功',
                        message: "变更任务状态成功"
                    })
                    this.handleSearch()
                }).catch(res => {
                    this.$notify.error({
                        title: '失败',
                        message: res.msg
                    })
                })
            })
                    .catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消变更任务状态'
                        });
                    });
        },
    },
  watch: {
    refresh: function (a, b) {
      if (a !== b && a) {
          this.handleSearch()
      }
    },
  }

}
</script>

<style scoped>

</style>
